﻿using Microsoft.AspNetCore.Mvc;
using MVC_da.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MVC_da.Controllers
{
    public class newsController : Controller
    {
        private readonly newsContext _db;
        public newsController(newsContext db)
        {
            _db = db;
        }

        [Route("/")]
        [Route("/index.html")]
        public IActionResult Index(int cid=0)
        {
            var list = _db.Categories.ToList();
            var xinwens = _db.Xinwens.
                OrderByDescending(x => x.Ntime).ToList();
            if(cid > 0)
            {
                xinwens = xinwens.Where(x => x.Cateid == cid).ToList();
            }
            ViewData["category"] = list;
            ViewData["xinwens"] = xinwens;
            ViewData["cid"] = cid;
            return View();
        }

        [Route("/detail.html")]
        public IActionResult Detail(int nno)
        {
            var xinwens = _db.Xinwens.FirstOrDefault(x => x.Nno == nno);
            ViewData["xinwens"] = xinwens;

            return View();
        }
    }
}
